<?php
/**
 * @Author: liuhongbin
 * @Email: 514660094@qq.com
 * @Date: 2019-02-12 16:15:10
 * @Description:
 * @LastEditors: liuhongbin
 * @LastEditTime: xxx
 */

namespace addons\cloud_market\controller\api\question;

use addons\cloud_market\controller\api\CloudMarketApiController;
use addons\cloud_market\model\CloudMarketQuestionAnswer;
use addons\cloud_market\model\CloudMarketQuestionCount;

class Count extends CloudMarketApiController
{
    protected $notApiCheck = [];
    protected $notApiLogin = [];

    /**
     * @var CloudMarketQuestionCount
     */
    protected $model;

    public function initialize()
    {
        parent::initialize();
        $this->model = new CloudMarketQuestionCount();
    }

    /**
     *
     * Create by :  刘泓宾<514660094@qq.com>
     * @param $id
     * @param $type
     * @return \think\response\Json
     */
    public function updateQuestionCount()
    {
        $answer_id = input('answer_id', 0);
        $question_id = input('question_id', 0);
        $type = input('type', 0);
        $data = ['user_id' => $this->userInfo['id'], 'answer_id' => $answer_id, 'question_id' => $question_id, 'type' => $type];
        $res = $this->model->where($data)->findOrEmpty();
        if ($res->isEmpty()) {
            $this->model->save($data);
        } else {
            $res->delete();
        }
        // 返回一下这个问题的统计信息
        $like_count = $this->model->where(['answer_id' => $answer_id, 'type' => $type])->count();
        (new CloudMarketQuestionAnswer())->where(['id'=>$answer_id])->save(['like_count'=>$like_count]);
        return $this->success("操作成功", ['like_count' => $like_count]);
    }

}
